import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { HttpServiceProvider } from '../../providers/http-service/http-service';
import { LoginPage }  from '../login/login';

@Component({
  selector: 'page-forget',
  templateUrl: 'forget.html',
})
export class ForgetPage {

  	 // 定时器
    private timer;
    sms = {text:'获取验证码', ok: false}
    user:any = {username:'', password:'', oldpassword:'', smscode:''}

  	constructor(public navCtrl: NavController, public navParams: NavParams,public http: HttpServiceProvider) {
  	}

    cowndown(){
      var d = 60
      this.timer = setInterval(() => {
        d--;
        if(d==0){
          this.sms.text = '获取验证码'
          this.sms.ok = false
          clearInterval(this.timer)
        }else{
          this.sms.text = '剩余 '+ d +'s';
          this.sms.ok = true
        }
      }, 1000);
    }

  	login(){
  		this.navCtrl.push(LoginPage)
  	}

    forget(){
    	if(this.user.username.length != 11 || this.user.username.slice(0,1)!='1'){
    		this.http.show('请输入正确的手机号')
    		return false
    	}
    	if(this.user.smscode.length != 6){
    		this.http.show('验证码不能为空')
    		return false
    	}
    	if(this.user.password.trim()!='' && 
    		this.user.password==this.user.oldpassword)
		      	this.http.forget(this.user, (result) => {
		        	if(result)
		          		this.navCtrl.push(LoginPage)
		      	})
		else
			this.http.show('密码不一致')
    }

    sendsms(){
      	if(this.user.username.length == 11 && this.user.username.slice(0,1)=='1'){
        	this.http.sendsms(this.user, (result) => {
	            if(result)
	              this.cowndown()
        	})
     	}else{
        	this.http.show('请输入正确的手机号')
      	}
    }

    
    
    ionViewDidLeave(){
      if (this.timer) {
        clearInterval(this.timer);
      }
    }

  	ionViewDidLoad() {
    	console.log('ionViewDidLoad ForgetPage');
  	}

}
